﻿import QtQuick 2.0

AnimatedImage {
    id: ghost
    source: "qrc:/img/img/" + type + ".gif"
    smooth: true

    property alias rotation: rotation
    property alias animMoving: animMoving
    property alias animDestroy: animDestroy
    property real sp: 0
    property real time: 10
    property string type: "ghost"
    property bool eat: true

//    onXChanged: {
//        if(x >= screenWidth/2-ghost.width/2 && x <= screenWidth/2-ghost.width/2)
//    }


    transform: Rotation{
        id: rotation
        axis.x: 0
        axis.y: 0
        axis.z: 1
        origin.x: ghost.width/2
        origin.y: ghost.height/2
    }

    MouseArea{
        anchors.fill: parent
        onClicked: {
            enabled = false
            animDestroy.restart()
        }
    }

    function pause(){
        eat = false
        animMoving.stop()
    }

    ParallelAnimation{
        id: animMoving
        NumberAnimation{target: ghost; property: "x"; to: screenWidth/2-ghost.width/2; duration: sp * time}
        NumberAnimation{target: ghost; property: "y"; to: screenHeight - mengRect.height/2-ghost.width/2; duration: sp * time}

        onStopped: {
            if(eat){
                lifeValue--
                deleteObjFromItems(ghost)
            }
        }
    }

    ParallelAnimation{
        id: animDestroy
        NumberAnimation{target: ghost; property: "opacity"; to: 0; duration: 500}
        onStarted: {
            score++
            eat = false
            animMoving.stop()
        }
        onStopped: {
            deleteObjFromItems(ghost)
        }
    }

}

